//index.js
//获取应用实例
var util = require('../../utils/util.js');
const app = getApp()
var pageNo = 1;
var hasNextPage = false;
Page({

  /**
   * 页面的初始数据  
   */
  data: {
    baseDomain: app.globalData.baseDomain,
    notice: null,
    goods: null,
    navs: [{
      image: app.globalData.baseDomain + '/images/brand/adidas.jpg'
    }, {
      image: app.globalData.baseDomain + '/images/brand/nike.jpg'
    }, {
      image: app.globalData.baseDomain + '/images/brand/gap.jpg'
    }, {
      image: app.globalData.baseDomain + '/images/brand/hm.jpg'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //调用获取首页公告接口
    wx.request({
      url: app.globalData.apiDomain + '/api/notice/getIndexNotice',
      success: function(res) {
        that.setData({
          notice: res.data.data
        });
      },
    });
    //调用获取商品列表接口
    wx.request({
      url: app.globalData.apiDomain + '/api/goods/getIndexGoods/1',
      success: function(res) {
        hasNextPage = res.data.data.hasNextPage;
        that.setData({
          goods: res.data.data.list,
        });
      },
      complete: function() {
        if (hasNextPage) {
          pageNo = 2;
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    //调用获取首页公告接口
    wx.request({
      url: app.globalData.apiDomain + '/api/notice/getIndexNotice',
      success: function(res) {
        that.setData({
          notice: res.data.data
        });
      },
    });
    //调用获取商品列表接口
    wx.request({
      url: app.globalData.apiDomain + '/api/goods/getIndexGoods/1',
      success: function(res) {
        hasNextPage = res.data.data.hasNextPage;
        that.setData({
          goods: res.data.data.list
        });
      },
      complete: function() {
        if (hasNextPage) {
          pageNo = 2;
        }
        wx.stopPullDownRefresh();
      },
    });
  },


  /**
   * 滑动到底部加载更多
   */
  onReachBottom: function() {
    var that = this;
    //调用获取商品列表接口
    if (hasNextPage) {
      wx.request({
        url: app.globalData.apiDomain + '/api/goods/getIndexGoods/' + pageNo,
        success: function(res) {
          hasNextPage = res.data.data.hasNextPage;
          var oldGoods = that.data.goods;
          var list = res.data.data.list;
          for (var i = 0; i < list.length; i++) {
            oldGoods.push(list[i]);
          }
          that.setData({
            goods: oldGoods
          });
        },
        complete: function() {
          if (hasNextPage) {
            pageNo++;
          }
        }
      });
    } else {
      // wx.showToast({
      //   title: '没有更多内容了',
      //   image: '../../images/not_happy_toast.png',
      //   duration: 1000,
      //   mask: true
      // })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  navigateToDetail: util.throttle(function(event) {
    wx.navigateTo({
      url: '../detail/detail?goodsId=' + event.currentTarget.dataset.goodsid,
    })
  }),

  navigateToBrandList: util.throttle(function() {
    wx.navigateTo({
      url: '../brand/brand',
    })
  }),
  navigateToNotice: util.throttle(function(e) {
    var id = e.currentTarget.dataset.noticeId;
    var isJump = e.currentTarget.dataset.isJump;
    var title = e.currentTarget.dataset.title;
    if (isJump == 1) {
      wx.navigateTo({
        url: '../noticeDetail/noticeDetail?id=' + id,
      })
    }
  })

})